package com.lzy.mall.seckill.interceptor;

import com.lzy.mall.common.constant.AuthServerConstant;
import com.lzy.mall.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzy
 * @date 2021/10/3 14:35
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
            //登录
            loginUser.set(attribute);
            return true;
        }else {
            response.sendRedirect("http://auth.lzyan.com/login.html");
            return false;
        }
    }
}
